package com.zret.oasys.operation.dept;
/**
 * 删除部门的方法
 */
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zret.oasys.entity.Dept;

public class DeleteDepts implements DeptServletOperation 
{
	@Override
	public void doOperation(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		//通过部门ID删除部门
		//1编码
		response.setContentType("application/json;charset=utf-8");
		//2从jsp获取deptid，获取查询的list
		String deptid = request.getParameter("deptid");
		
		//如果传输的是空值，查询所有部门
		if ("".equals(deptid))
		{
			List<Dept> list = deptservice.getDepts();
			//3创建输出流
			PrintWriter out = response.getWriter();
			//4创建Gson对象
			Gson gson = new Gson();
			//5将查询到的部门list转为字符串
			String jsonString = gson.toJson(list);
			//6控制台测试打印输出
			System.out.println(jsonString);
			//7传输给jsp页面
			out.print(jsonString);
			//8关闭out
			out.close();
		}
		else
		{
			int id =Integer.valueOf(deptid);
			//调用删除部门的方法，根据部门id
			boolean flag = deptservice.deleteDepts(id);
			//3创建Gson对象
			PrintWriter out = response.getWriter();
			//4创建Gson对象
			Gson gson = new Gson();
			//5控制台打印输出
			String msg1 = gson.toJson("删除部门成功");
			String msg2 = gson.toJson("删除部门失败");
			//6判断是否删除成功
			if (flag) 
			{
				out.print(msg1);
			}
			else 
			{
				out.print(msg2);
			}
			//7关闭out
			out.close();
		}
	}
}
